<?php
error_reporting(E_ALL);
  ini_set('display_errors', TRUE);
  ini_set('display_startup_errors', TRUE);
  date_default_timezone_set('Europe/London');
  if (PHP_SAPI == 'cli')
     die('This example should only be run from a Web Browser');
  require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
  $objPHPExcel = new PHPExcel();
  $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
        ->setLastModifiedBy("Maarten Balliauw")
        ->setTitle("Office 2007 XLSX Test Document")
        ->setSubject("Office 2007 XLSX Test Document")
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("Test result file");
  $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '序号')
            ->setCellValue('B1', '学号')
            ->setCellValue('C1', '姓名')
   ->setCellValue('D1', '部门')
            ->setCellValue('E1', '工作量');
   
  // $con = mysql_connect("localhost","root","123");
  // if (!$con)
  // {
        // die('Could not connect: ' . mysql_error());
  // }
  // mysql_select_db("outuser", $con);     
  // mysql_query("set names 'gbk'");
  // $sql="SELECT * FROM work WHERE state='1' ORDER BY id desc";     
  // $result=mysql_query($sql); 
  $result = array(array('id'=>1,'user'=>'YQJ','name'=>'男','work'=>24,'department'=>24));
  $i = 2;
  $id = 1;
  foreach ($result as $row) 
  // while($row=mysql_fetch_array($result))
  { 
 // var_dump($row['department']);die;
        switch($row['department'])
        {
            case "content":
                $row['department'] = "内容部";
                break;
            case "operate":
                $row['department'] = "运营部";
                break;
            case "front":
                $row['department'] = "技术部前端";
                break;
            case "backstage":
                $row['department'] = "技术部后台服务器";
                break;
            case "move":
                $row['department'] = "技术部移动";
                break;
            case "page":
                $row['department'] = "技术部页面";
                break;
        }
        $user = $row['user'];
        $name = $row['name'];
        $work = $row['work'];
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.$i, $id)
                    ->setCellValue('B'.$i, $user)
                    ->setCellValue('C'.$i, $name)
                    ->setCellValue('D'.$i, $row['department'])
                    ->setCellValue('E'.$i, $work);
        
        $objPHPExcel->getActiveSheet()
                    ->getCell('B'.$i)->setValueExplicit($user, PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()
                    ->getCell('C'.$i)->setValueExplicit($name, PHPExcel_Cell_DataType::TYPE_STRING);
        $objPHPExcel->getActiveSheet()
                    ->getCell('E'.$i)->setValueExplicit($work, PHPExcel_Cell_DataType::TYPE_STRING);
           
        $i++;
        $id++; 
    }
   // mysql_close($con);
 
  $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
 
  $objPHPExcel->getActiveSheet()->setTitle('Simple');
  $objPHPExcel->setActiveSheetIndex(0);
  header("Content-Type: text/html;charset=gbk");
  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment;filename="sss.xlsx"');
  header('Cache-Control: max-age=0');
  
  header('Cache-Control: max-age=1');
  header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 
  header ('Cache-Control: cache, must-revalidate'); 
  header ('Pragma: public');
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  $objWriter->save('php://output');
